package com.egao.common.core.shiro;

import com.egao.common.system.entity.User;
import com.egao.common.system.entity.UserTwo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.apache.shiro.session.mgt.eis.MemorySessionDAO;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.session.mgt.ServletContainerSessionManager;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;

/**
 * author: zhaopeng wang
 * date: 2022-03-13 14:41
 * description: ShiroUtil
 */

public class ShiroUtil {
    /**
     * @功能描述：   单用户登录,清除当前用户以前登录时保存的session会话
     * @param loginName
     */


    public void singleUseLogin(String loginName){
        // 1.获取当前用户sessionId
        String currentUserSessionId = SecurityUtils.getSubject().getSession().getId().toString();
        Session session = SecurityUtils.getSubject().getSession();
        // 2.获取当前用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        // 3.获取shiro的sessionManager
        DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
        //ServletContainerSessionManager sessionManager = (ServletContainerSessionManager)securityManager.getSessionManager();
        SessionDAO sessionDAO = new MemorySessionDAO();
        sessionDAO.delete(session);
        // 4.获取所有已登录用户的session列表

        //MemorySessionDAO sessionDAO = new MemorySessionDAO();
        Collection<Session> sessions = sessionDAO.getActiveSessions();


//        Collection<Object> attributeKeys = sessionManager.getSession().getAttributeKeys();
//        Collection<Session> sessions = sessionManager.getSessionDAO().getActiveSessions();

        if (sessions.size() > 0) {
            System.out.println("仅允许单用户登录,开始清理遗留用户信息~");
            for(Session onlineSession:sessions){
                // 5. 清除当前用户以前登录时保存的session会话
                if (loginName.equals(user.getUsername()) && !onlineSession.getId().equals(currentUserSessionId)) {
                    sessionDAO.delete(onlineSession);
                    System.out.println("清理用户["+loginName+"],SessionId为["+onlineSession.getId()+"]的Session信息!");
                }
            }
        } else {
            System.out.println("无可清理用户信息~");
        }
    }

    public void singleUseTwoLogin(String loginName){
        // 1.获取当前用户sessionId
        String currentUserSessionId = SecurityUtils.getSubject().getSession().getId().toString();
        // 2.获取当前用户
        UserTwo user = (UserTwo) SecurityUtils.getSubject().getPrincipal();
        // 3.获取shiro的sessionManager
        DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
        DefaultWebSessionManager sessionManager = (DefaultWebSessionManager)securityManager.getSessionManager();
        // 4.获取所有已登录用户的session列表
        Collection<Session> sessions = sessionManager.getSessionDAO().getActiveSessions();

        if (sessions.size() > 1) {
            System.out.println("仅允许单用户登录,开始清理遗留用户信息~");
            for(Session onlineSession:sessions){
                // 5. 清除当前用户以前登录时保存的session会话
                if (loginName.equals(user.getUsername()) && !onlineSession.getId().equals(currentUserSessionId)) {
                    sessionManager.getSessionDAO().delete(onlineSession);
                    System.out.println("清理用户["+loginName+"],SessionId为["+onlineSession.getId()+"]的Session信息!");
                }
            }
        } else {
            System.out.println("无可清理用户信息~");
        }
    }
}
